<template>
    <div class="edit-popup">
        <popup
            ref="popupRef"
            :title="popupTitle"
            :async="true"
            width="550px"
            @confirm="handleSubmit"
            @close="handleClose"
        >
            <el-form ref="formRef" :model="formData" label-width="130px" :rules="formRules">
                <el-form-item label="套餐名称" prop="name">
                    <el-input v-model="formData.name" clearable placeholder="请输入套餐名称" />
                </el-form-item>
                <el-form-item label="实际售价(单位元)" prop="real_price">
                    <el-input-number
                        v-model="formData.real_price"
                        clearable
                        placeholder="请输入实际售价(单位元)"
                        @input="handleRealPriceInput"
                        :min="0" :max="9999"
                    />
                </el-form-item>
                <el-form-item label="划线价(单位元)" prop="original_price">
                    <el-input-number
                        v-model="formData.original_price"
                        clearable
                        placeholder="请输入划线价(单位元)"
                        @input="handleOriginalPriceInput"
                        :min="0" :max="9999"
                    />
                </el-form-item>
                <el-form-item label="购买点券" prop="buy_cost">
                    <div>
                        <el-input-number v-model="formData.buy_cost" :min="0" :max="9999" />
                        <div class="form-tips"></div>
                    </div>
                </el-form-item>
                <el-form-item label="赠送点券" prop="send_cost">
                    <div>
                        <el-input-number v-model="formData.send_cost" :min="0" :max="9999" />
                        <div class="form-tips"></div>
                    </div>
                </el-form-item>
                <el-form-item label="排序" prop="sort">
                    <div>
                        <el-input-number v-model="formData.sort" :min="0" :max="9999" />
                        <div class="form-tips">默认为0， 数值越大越排前</div>
                    </div>
                </el-form-item>
                <el-form-item label="状态" prop="is_show">
                    <el-switch v-model="formData.is_show" :active-value="1" :inactive-value="0" />
                </el-form-item>
                <el-form-item label="活动时间">
                    <daterange-picker
                        v-model:startTime="formData.start_time"
                        v-model:endTime="formData.end_time"
                    />
                </el-form-item>
            </el-form>
        </popup>
    </div>
</template>

<script lang="ts" setup name="interfaceGoodsSaleEdit">
import type { FormInstance } from 'element-plus'
import Popup from '@/components/popup/index.vue'
import {
    apiInterfaceGoodsSaleAdd,
    apiInterfaceGoodsSaleEdit,
    apiInterfaceGoodsSaleDetail
} from '@/api/interface_goods_sale'
import { timeFormat } from '@/utils/util'
import type { PropType } from 'vue'
defineProps({
    dictData: {
        type: Object as PropType<Record<string, any[]>>,
        default: () => ({})
    }
})
const emit = defineEmits(['success', 'close'])
const formRef = shallowRef<FormInstance>()
const popupRef = shallowRef<InstanceType<typeof Popup>>()
const mode = ref('add')

// 弹窗标题
const popupTitle = computed(() => {
    return mode.value == 'edit' ? '编辑接口套餐售卖' : '新增接口套餐售卖'
})

// 表单数据
const formData = reactive({
    id: '',
    name: '',
    real_price: '',
    original_price: '',
    buy_cost: '',
    send_cost: '',
    sort: 0,
    is_show: 1,
    start_time: '',
    end_time: ''
})

// 价格输入过滤
const handleRealPriceInput = (value: string) => {
    formData.real_price = value.replace(/\D/g, '') // 过滤非数字字符
}
const handleOriginalPriceInput = (value: string) => {
    formData.original_price = value.replace(/\D/g, '') // 过滤非数字字符
}

// 表单验证
const formRules = reactive<any>({
    name: [
        {
            required: true,
            message: '请输入套餐名称',
            trigger: ['blur']
        }
    ],
    real_price: [
        {
            required: true,
            message: '请输入实际售价(单位分)',
            trigger: ['blur']
        }
    ],
    original_price: [
        {
            required: true,
            message: '请输入划线价(单位分)',
            trigger: ['blur']
        }
    ],
    buy_cost: [
        {
            required: true,
            message: '请输入购买点券',
            trigger: ['blur']
        }
    ],
    send_cost: [
        {
            required: true,
            message: '请输入赠送点券',
            trigger: ['blur']
        }
    ],
    sort: [
        {
            required: true,
            message: '请输入排序',
            trigger: ['blur']
        }
    ],
    start_time: [
        {
            required: true,
            message: '请输入开始时间',
            trigger: ['blur']
        }
    ],
    end_time: [
        {
            required: true,
            message: '请输入结束时间',
            trigger: ['blur']
        }
    ]
})

// 获取详情
const setFormData = async (data: Record<any, any>) => {
    for (const key in formData) {
        if (data[key] != null && data[key] != undefined) {
            //@ts-ignore
            formData[key] = data[key]
        }
    }
}

const getDetail = async (row: Record<string, any>) => {
    const data = await apiInterfaceGoodsSaleDetail({
        id: row.id
    })
    setFormData(data)
}

// 提交按钮
const handleSubmit = async () => {
    await formRef.value?.validate()
    const data = { ...formData }
    mode.value == 'edit'
        ? await apiInterfaceGoodsSaleEdit(data)
        : await apiInterfaceGoodsSaleAdd(data)
    popupRef.value?.close()
    emit('success')
}

//打开弹窗
const open = (type = 'add') => {
    mode.value = type
    popupRef.value?.open()
}

// 关闭回调
const handleClose = () => {
    emit('close')
}

defineExpose({
    open,
    setFormData,
    getDetail
})
</script>
